﻿using System.Windows;
using System.Windows.Controls;
using AstekServis.Web.Data;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;
using System.Windows.Printing;
using DevExpress.AgDataGrid;
using System.Collections.ObjectModel;
using System.Windows.Media;

namespace AstekServis.Views
{
    public partial class RaporView : ChildWindow
    {
        public string GridData { get; set; }
        public string GrafikData { get; set; }
        public int RaporTipi { get; set; }
        public int GrafikTipi { get; set; }
        public string GrafikAlan { get; set; }
        public string GrafikDeger { get; set; }
        public ModemDurumu[] ModemData { get; set; }

        public RaporView()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Rapor_Loaded);
        }

        void Rapor_Loaded(object sender, RoutedEventArgs e)
        {
            chart1.Title = this.Title;
            Bussiness.Methods mtd = new Bussiness.Methods();
            bool grid = false;
            bool chart = false;
            bool modem = false;

            switch (RaporTipi)
            {
                case 0:
                    grid = true;
                    break;
                case 1:
                    chart = true;
                    break;
                case 2:
                    grid = true;
                    chart = true;
                    break;
                case 3:
                    modem = true;
                    break;
            }

            if (modem)
            {
                grdModem.ItemsSource = ModemData;
                tabItem1.Visibility = System.Windows.Visibility.Collapsed;
                tabItem2.Visibility = System.Windows.Visibility.Collapsed;
                tabControl1.SelectedIndex = 2;
                grdResult.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                tabItem3.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (grid)
            {
                if (!string.IsNullOrEmpty(GridData))
                {
                    object[] rows = mtd.CreateItemsSource(GridData);
                    //grdResult.ColumnsAutoWidth = true;
                    grdResult.ItemsSource = rows;
                }
            }

            if (chart)
            {
                if (!string.IsNullOrEmpty(GrafikData))
                {
                    object[] rows = mtd.CreateItemsSource(GrafikData);
                    chart1.Series.RemoveAt(0);
                    if (GrafikTipi == 0)
                    {
                        LineSeries sr = new LineSeries();
                        sr.ItemsSource = rows;
                        sr.DependentValueBinding = new Binding(GrafikDeger);
                        sr.IndependentValueBinding = new Binding(GrafikAlan);
                        chart1.Series.Add(sr);
                    }
                    else if (GrafikTipi == 1)
                    {
                        ColumnSeries sr = new ColumnSeries();
                        sr.ItemsSource = rows;
                        sr.DependentValueBinding = new Binding(GrafikDeger);
                        sr.IndependentValueBinding = new Binding(GrafikAlan);
                        chart1.Series.Add(sr);
                    }
                    else if (GrafikTipi == 2)
                    {
                        PieSeries sr = new PieSeries();
                        sr.ItemsSource = rows;
                        sr.DependentValueBinding = new Binding(GrafikDeger);
                        sr.IndependentValueBinding = new Binding(GrafikAlan);
                        chart1.Series.Add(sr);
                    }
                }
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void yeniServis_MouseLeftButtonUp_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var ctl = e.OriginalSource as Image;
            if (null != ctl)
            {
                var modem = ctl.DataContext as ModemDurumu;
                if (null != modem)
                {
                    yeniServis frm = new yeniServis();
                    frm.AracBul(modem.Plaka.Replace("\n", ""));
                    frm.Show();
                }
            }
        }

        private void aracBilgi_MouseLeftButtonUp_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var ctl = e.OriginalSource as Image;
            if (null != ctl)
            {
                var modem = ctl.DataContext as ModemDurumu;
                if (null != modem)
                {
                    BilgiEkrani frm = new BilgiEkrani();
                    frm.AracKodu = modem.AracKodu;
                    frm.FirmaAdi = modem.Firma;
                    frm.GrupAdi = modem.Grup;
                    frm.Adres = modem.Adres;
                    frm.Show();
                }
            }
        }

        private void btnExcell_Click(object sender, RoutedEventArgs e)
        {
            ExcellExporter.ExportDataGrid(grdResult);
        }

        private void btnYazdir_Click(object sender, RoutedEventArgs e)
        {
            //PrintDocument docDataGrid = new PrintDocument();
            //docDataGrid.PrintPage += docDataGrid_PrintPage;
            //docDataGrid.Print("Test");

            //grdResult.Print("My Print Document", HorizontalAlignment.Stretch,
            //    VerticalAlignment.Stretch, new Thickness(0), true, true, null);

            //PrintDocument printDocument = new PrintDocument();


            //printDocument.PrintPage += (s, r) =>
            //{

            //    r.PageVisual = grdResult;

            //    var transformGroup = new TransformGroup();

            //    transformGroup.Children.Add(new RotateTransform() { Angle = 90 });

            //    transformGroup.Children.Add(new TranslateTransform() { X = r.PrintableArea.Width });

            //    LayoutRoot.RenderTransform = transformGroup;

            //};

            //printDocument.EndPrint += (s, r) =>
            //{

            //    var transformGroup = new TransformGroup();

            //    transformGroup.Children.Add(new RotateTransform() { Angle = 0 });

            //    transformGroup.Children.Add(new TranslateTransform() { X = 0 });

            //    LayoutRoot.RenderTransform = transformGroup;

            //};

            //printDocument.Print("");

            PrintHelper.PrintElement("Rapor", grdResult, "bitti");
        }

        void docDataGrid_PrintPage(object sender, PrintPageEventArgs e)
        {

            // Stretch to the size of the printed page


            grdResult.Width = e.PrintableArea.Width;

            grdResult.Height = e.PrintableArea.Height;

            // Assign the XAML element to be printed

            e.PageVisual = grdResult;

            // Specify whether to call again for another page

            e.HasMorePages = false;

        }

    }
}

